﻿/*
-------------------------------------------------------------------------------------------------------------
JS Class 
Name:zsi.Places
Description: 
    This will display the list of places("region,province,city/municipality,barangay,sitio/purok").
    Once you selected any of these places as mentioned above, the change event of dropdownlist 
    will get fired then display the list of items on another dropdownlist.
    
Developer : Date Modified :  Reason
    GMF   : 08/25/2010    :  Fixed on zsi.Places.RaiseEventOnPlaceChange, clears all sub dropdownlist if parent place id equal to zero.
    GMF   : 06/21/2010    :  Started the Places class          
    GMF   : 07/07/2010    :  changed into dynamic objects.   
    GMF   : 07/11/2010    :  added: checking callback function and validate the response data.                  
----------------------------------------------------------------------------------------------
*/
zsi.Places = function(Region, Province, CityMunicipality, Barangay, SitioPurok) {
    var parentObject = this;
    this.Region = new zsi.Places.PlaceInfo(Region);
    this.Province = new zsi.Places.PlaceInfo(Province);
    this.CityMunicipality = new zsi.Places.PlaceInfo(CityMunicipality);
    this.Barangay = new zsi.Places.PlaceInfo(Barangay);
    this.SitioPurok = new zsi.Places.PlaceInfo(SitioPurok);

    //alert(this.CityMunicipality.object.name);
    
    this.Region.object.change(function() { zsi.Places.RaiseEventOnPlaceChange(parentObject, "region"); });
    this.Province.object.change(function() { zsi.Places.RaiseEventOnPlaceChange(parentObject, "province"); });
    this.CityMunicipality.object.change(function() { zsi.Places.RaiseEventOnPlaceChange(parentObject, "citymunicipality"); });
    this.Barangay.object.change(function() { zsi.Places.RaiseEventOnPlaceChange(parentObject, "barangay"); });
    this.SitioPurok.object.change(function() { zsi.Places.RaiseEventOnPlaceChange(parentObject, "sitiopurok"); });

    this.Init = function() {
        zsi.Util.Common.LoadingBar(true);
        var _Parent = this;
        $.getJSON(BASE_URL + "Places/GetRegionList", null, function(data, status) {
            if (status == "success") {
                if (typeof (data) == "object") {
                    _Parent.Region.object.fillSelect(data);
                    _Parent.OnComplete(_Parent.Region);
                } else {
                    zsi.Util.Common.LoadingBar(false);
                    alert("zsi.places.js: " + data);

                }
            }
            else {
                zsi.Util.Common.LoadingBar(false);
                alert("Failed:" + data);
            }
        });
    }

    this.OnComplete = function(callBackFunction) {
        if (typeof (OnLoadPlacesComplete) != "undefined") {
            OnLoadPlacesComplete(callBackFunction)
        }

    }

}

zsi.Places.PlaceInfo=function(name) {
    this.name = name
    this.object = $("#" + name);
}

zsi.Places.RaiseEventOnPlaceChange = function(parentObject, name) {
    var url = "";
    var isNameFound = true;
    var TargetObject = null;
    var CurrentObject = null;
    var _value = "";
    switch (name) {
        case "region":
            _value = parentObject.Region.object.val();
            parentObject.CityMunicipality.object.clearSelect();
            parentObject.Province.object.clearSelect();
            parentObject.CityMunicipality.object.clearSelect();
            parentObject.Barangay.object.clearSelect();
            parentObject.SitioPurok.object.clearSelect();
            if (_value == 0) { isNameFound = false; break; } 
            zsi.Util.Common.LoadingBar(true);
            url = BASE_URL + "Places/GetProvinceList/?p_RegionId=" + _value;

            TargetObject = parentObject.Province;
            CurrentObject = parentObject.Region;
            break;
        case "province":
            _value = parentObject.Province.object.val();
            TargetObject = parentObject.CityMunicipality;
            if (TargetObject.name == null) { isNameFound = false; break; }
            parentObject.CityMunicipality.object.clearSelect();
            parentObject.Barangay.object.clearSelect();
            parentObject.SitioPurok.object.clearSelect();
            if (_value == 0) { isNameFound = false; break; }
            zsi.Util.Common.LoadingBar(true);
            url = BASE_URL + "Places/GetCityMunicipalityList/?p_ProvinceId=" + _value;
            CurrentObject = parentObject.Province;
            break;
        case "citymunicipality":
            _value = parentObject.CityMunicipality.object.val();
            TargetObject = parentObject.Barangay;
            if (TargetObject.name == null) { isNameFound = false; break; }
            parentObject.Barangay.object.clearSelect();
            parentObject.SitioPurok.object.clearSelect();
            if (_value == 0) { isNameFound = false; break; }         
            zsi.Util.Common.LoadingBar(true);
            url = BASE_URL + "Places/GetBarangayList/?p_CityMunicipalityId=" + _value;
            CurrentObject = parentObject.CityMunicipality;
            break;
        case "barangay":
            _value = parentObject.Barangay.object.val();
            TargetObject = parentObject.SitioPurok;
            if (TargetObject.name == null) { isNameFound = false; break; }            
            parentObject.SitioPurok.object.clearSelect();
            if (_value == 0) { isNameFound = false; break; }
            zsi.Util.Common.LoadingBar(true);
            url = BASE_URL + "Places/GetSitioPurokList/?p_BarangayId=" + _value;
            CurrentObject = parentObject.Barangay;
            break;
        case "sitiopurok": isNameFound = false; break;
        default: isNameFound = false; break;
    }

    if (isNameFound) {
        $.getJSON(url, null, function(data, status) {
            if (status == "success") {
                if (typeof (data) == "object") {
                    TargetObject.object.fillSelect(data);
                    parentObject.OnComplete(TargetObject);
                } else {
                    zsi.Util.Common.LoadingBar(false);
                    alert("zsi.places.js: " + data);
                }
            }
            else {
                zsi.Util.Common.LoadingBar(false);
                alert("Failed:" + data);

            }
        });
    }
    else {
        zsi.Util.Common.LoadingBar(false);
    }

}



